package cn.edu.zjicm.wordsnet_d.adapter;

import java.util.ArrayList;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.LeaderboardActivity;
import cn.edu.zjicm.wordsnet_d.data.BoardList;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.download.JudgeNetwork;
import cn.edu.zjicm.wordsnet_d.synchronize.LeaderBoardInterface;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;

public class SchoolRankPagerAdapter extends PagerAdapter {
	private LeaderboardActivity mContext;
	public static ArrayList<OneWord> wordList = new ArrayList<OneWord>();

	private TextView titleTv1, titleTv2, titleTv3, titleTv4;
	private ListView[] listView = new ListView[3];
	private ViewFlipper[] flipper = new ViewFlipper[3];
	private SchoolRankAdapter[] schoolRankAdapter = new SchoolRankAdapter[3];
	private View[] footerView = new View[3];
	private TextView[] footerText = new TextView[3];
	private BoardList boardList;
	private boolean isFake;

	public SchoolRankPagerAdapter(LeaderboardActivity context, BoardList boardList, boolean isFake) {
		mContext = context;
		this.boardList = boardList;
		this.isFake = isFake;
	}

	@Override
	public int getCount() {
		return 3;
	}

	@Override
	public boolean isViewFromObject(View view, Object obj) {
		return view == obj;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View mView = init(container, position);
		return mView;
	}

	private View init(ViewGroup container, int position) {
		int rankIndex = 0;

		View view = LayoutInflater.from(mContext).inflate(R.layout.view_school_rank_page, null);
		flipper[position] = (ViewFlipper) view.findViewById(R.id.rank_page_flipper);
		flipper[position].setOnClickListener(flipperListener);

		if (!JudgeNetwork.getInstance(mContext).isMobileConnected() && !JudgeNetwork.getInstance(mContext).isWifiConnected()) {
			flipper[position].setDisplayedChild(1);
		} else {
			flipper[position].setDisplayedChild(0);
		}

		titleTv1 = (TextView) view.findViewById(R.id.rank_title_tv1);
		titleTv2 = (TextView) view.findViewById(R.id.rank_title_tv2);
		titleTv3 = (TextView) view.findViewById(R.id.rank_title_tv3);
		titleTv4 = (TextView) view.findViewById(R.id.rank_title_tv4);

		switch (position) {
		case 0:
			rankIndex = SchoolRankAdapter.INDEX_PUNCHOUT;
			titleTv2.setVisibility(View.VISIBLE);
			titleTv1.setText("排名");
			titleTv2.setText("头像");
			titleTv3.setText("昵称");
			titleTv4.setText("打卡天数");
			titleTv4.setVisibility(View.VISIBLE);
			titleTv2.setVisibility(View.GONE);
			break;
		case 1:
			rankIndex = SchoolRankAdapter.INDEX_WORD;
			titleTv2.setVisibility(View.VISIBLE);
			titleTv1.setText("排名");
			titleTv2.setText("头像");
			titleTv3.setText("昵称");
			titleTv4.setText("经验值");
			titleTv4.setVisibility(View.VISIBLE);
			titleTv2.setVisibility(View.GONE);
			break;
		case 2:
			rankIndex = SchoolRankAdapter.INDEX_SCHOOL;
			titleTv1.setText("排名");
			titleTv3.setText("学校");
			titleTv4.setText("人数");
			titleTv2.setVisibility(View.GONE);
			break;
		}

		schoolRankAdapter[position] = new SchoolRankAdapter(mContext, rankIndex, boardList, isFake);
		listView[position] = (ListView) view.findViewById(R.id.rank_listview);
		addFooterView(position);
		listView[position].setAdapter(schoolRankAdapter[position]);
		((ViewGroup) container).addView(view);
		return view;
	}

	/**
	 * 在有footView时ListView的footerDividersEnabled属性才生效，所以添加footView并设置不可见
	 * 
	 */
	private void addFooterView(int position) {
		footerView[position] = LayoutInflater.from(mContext).inflate(R.layout.view_leader_board_foot_view, null);
		footerText[position] = (TextView) footerView[position].findViewById(R.id.footer_view_tv);
		listView[position].addFooterView(footerView[position], null, false);
		footerView[position].setVisibility(View.GONE);
	}

	private void setFootViewVisible(int position, boolean addPaddingTop) {
		if (addPaddingTop) {
			footerText[position].setPadding(0, 400, 0, 0);
		}
		footerView[position].setVisibility(View.VISIBLE);
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public CharSequence getPageTitle(int position) {
		switch (position) {
		case 0:
			return "打卡榜";
		case 1:
			return "单词学习榜";
		case 2:
			return "学校榜";
		}
		return super.getPageTitle(position);
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	public void updateListData(BoardList boardList, int rankIndex) {
		if (rankIndex == 0 || rankIndex == 1) {
			if (!boardList.isMyDataOnBoard()) {
				if (boardList.getBoardList().size() == 0) {
					setFootViewVisible(rankIndex, true);
				} else {
					setFootViewVisible(rankIndex, false);
				}

			} else {
				if (listView[rankIndex].getFooterViewsCount() > 0) {
					footerView[rankIndex].setVisibility(View.GONE);
					// listView[rankIndex].removeFooterView(footerView[rankIndex]);
				}
			}
		}
		schoolRankAdapter[rankIndex].updateListData(boardList);
	}

	public void showBadNetwork(int position) {
		if (flipper[position] != null) {
			flipper[position].setDisplayedChild(1);
		}
	}

	public void showGoodNetwork(int position) {
		if (flipper[position] != null) {
			flipper[position].setDisplayedChild(0);
		}
	}

	OnClickListener flipperListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			for (int i = 0; i < 3; i++) {
				flipper[i].setDisplayedChild(0);
			}
			if (v == flipper[0]) {
				mContext.toGetUserBoard(LeaderBoardInterface.PUNCH_BOARD);
			} else if (v == flipper[1]) {
				mContext.toGetUserBoard(LeaderBoardInterface.EXP_BOARD);
			} else if (v == flipper[2]) {
				mContext.toGetSchoolBoard();
			}
		}
	};
}
